﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.system
{
    public partial class myaccount : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";

            //构造SQL语句
            sql = @"select a.userid, a.syspassword, a.sysaccount, a.username, a.mobile, a.email, a.stu1, a.remark, a.sysdate, b.jobid, b.jobremark, c.departmentid, c.departmentremark, e.rulegroupid, e.rulegroupremark
                            from sysuser a, job b, department c, sysuserrule d, rulegroup e
                              where a.departmentid=c.departmentid 
                                and a.jobid=b.jobid
                                and a.userid = d.userid 
                                and d.rulegroupid=e.rulegroupid
                                and a.userid='{0}'";

            sql = String.Format(sql, Session["userid"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_mobile.Text = dr["mobile"].ToString();
                txt_email.Text = dr["email"].ToString();
                txt_remark.Text = dr["remark"].ToString();

                txt_username.Text = dr["username"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            alert_msg = "";

            edowork.com.Model.sysuserInfo sysuserInfo = new sysuserInfo();

            sysuserInfo.Username = txt_username.Text.Trim();
            sysuserInfo.Mobile = txt_mobile.Text.Trim();
            sysuserInfo.Email = txt_email.Text.Trim();
            sysuserInfo.Remark = txt_remark.Text.Trim();

            string oldpassword = txt_oldpassword.Text.Trim();
            string newpassword = txt_newpassword.Text.Trim();
            string confirpassword = txt_confirpassword.Text.Trim();

            //当用户输入旧密码或新密码，则表示用户修改密码，此时需检查三组密码是否都同时输入
            if ((String.IsNullOrEmpty(oldpassword) == false) || (String.IsNullOrEmpty(newpassword) == false) || (String.IsNullOrEmpty(confirpassword) == false))
            {
                if (String.IsNullOrEmpty(oldpassword))
                {
                    alert_msg = String.Format(Scripts.alert_warning, "请输入原密码");
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                    txt_oldpassword.Focus();
                    return;
                }

                if (String.IsNullOrEmpty(newpassword))
                {
                    alert_msg = String.Format(Scripts.alert_warning, "请输入新密码");
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                    txt_newpassword.Focus();
                    return;
                }

                if (String.IsNullOrEmpty(confirpassword))
                {
                    alert_msg = String.Format(Scripts.alert_warning, "请输入新密码");
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                    txt_confirpassword.Focus();
                    return;
                }

                if (newpassword != confirpassword)
                {
                    alert_msg = String.Format(Scripts.alert_warning, "两次输入的新密码不一致");
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                    txt_newpassword.Focus();
                    return;
                }
            }

            if (String.IsNullOrEmpty(sysuserInfo.Username))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入用户名称");
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                txt_username.Focus();
                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入sysuser表
                if (String.IsNullOrEmpty(newpassword) == false)
                {
                    sql = "update sysuser set syspassword='{2}', mobile='{3}', email='{4}', remark='{5}', username='{6}' where userid='{0}' and syspassword='{1}'";
                    sql = String.Format(sql, Session["userid"].ToString(), SysID.md532(oldpassword), SysID.md532(newpassword), sysuserInfo.Mobile, sysuserInfo.Email, sysuserInfo.Remark, sysuserInfo.Username);
                }
                else
                {
                    sql = "update sysuser set mobile='{1}', email='{2}', remark='{3}', username='{4}' where userid='{0}'";
                    sql = String.Format(sql, Session["userid"].ToString(), sysuserInfo.Mobile, sysuserInfo.Email, sysuserInfo.Remark, sysuserInfo.Username);
                }

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }



            
        }

    }
}